package com.gurun.codes.business.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.gurun.base.Logger;
import com.gurun.codes.R;
import com.gurun.codes.business.common.CommonTipsDialogActivity;
import com.gurun.gateway.MainService;
import com.gurun.model.bean.event_bean.PushMessageEvent;
import com.gurun.model.bean.home.DeviceStatusUpdateModel;

import org.greenrobot.eventbus.EventBus;

import cn.jpush.android.api.JPushInterface;

/**
 * 极光消息推送接收器
 */
public class JpushRecevied extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Logger.d("JPush用户注册成功,regId:" + regId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {//自定义消息
            String message = bundle.getString(JPushInterface.EXTRA_EXTRA);
            String msg_content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String regId = JPushInterface.getRegistrationID(context);
            Logger.d("收到的自定义消息:" + message + ",regId:" + regId + ",msg_content:" + msg_content);
            processCustomMessage(context, message, msg_content);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {//普通广播通知
            Logger.d("接受到推送下来的通知");
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Logger.d("用户点击打开了通知");
        } else {
            Logger.d("Unhandle intent - " + intent.getAction());
        }

    }

    /**
     * @param message
     */
    private void processCustomMessage(Context context, String message, String msg_content) {
        if ("smartDeviceStatusUpdate".equals(msg_content)) {//智能设备状态更新
            if (!TextUtils.isEmpty(message)) {
                try {
                    DeviceStatusUpdateModel bean = new Gson().fromJson(message, DeviceStatusUpdateModel.class);
                    if (bean != null) {
                        if (bean.getAlarmFlag() == 1 && !TextUtils.isEmpty(bean.getAlarmContent())) {
                            CommonTipsDialogActivity.startActivity(context, bean.getAlarmContent(), -1);
                        }
                        //通知设备状态更新
                        EventBus.getDefault().post(bean);
                    }
                } catch (Exception e) {

                }
            }
        } else if ("startCameraLiveVideo".equals(msg_content)) {//开启摄像头直播页面

        } else if ("forceLogout".equals(msg_content)) {//强制退出登录
            CommonTipsDialogActivity.startActivity(context, context.getResources().getString(R.string.logout_tips), CommonTipsDialogActivity.ACTION_LOGOUT);
        } else {
            EventBus.getDefault().post(new PushMessageEvent(message));
        }
    }
}